package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.po.DishPO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Content: 菜品管理
 * @Author: Asen
 * @Date: 2023/9/1 13:53
 */
@Slf4j
@Api(tags = "菜品相关接口")
@RestController("adminDishController")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO source){
        return Result.success(dishService.saveWithFlavor(source));
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO source){
        log.info("source:{}",source);
        return Result.success(dishService.page(source));
    }

    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        dishService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable("id")Long id){
        return Result.success(dishService.getByIdWithFlavor(id));
    }

    @PutMapping("")
    @ApiOperation("根据id修改菜品")
    public Result update(@RequestBody DishDTO source){
        dishService.update(source);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("更改菜品状态")
    public Result updateStatus(@PathVariable("status")Integer status,Long id){
        return Result.success(dishService.updateStatus(status,id));
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishPO>> list(Long categoryId){
        return Result.success(dishService.selectByCategoryId(categoryId));
    }
}
